var prefix = "http://www.51rrkan.com/";
var preLen = prefix.length;

function extractKeyFromURL(URL) {
	console.log("extractKeyFromURL:" + URL);
	// remove prefix from URL, 
	//result: http://www.51rrkan.com/omdsj/jiushiji/ --> omdsj/jiushiji/
	key = URL.substring(preLen, URL.length);
	
	
	if(key.lastIndexOf("/") == (key.length - 1)) 
		key = key.substring(0, key.length - 1);
	
	var infos = key.split("/");
	
	if(infos.length != 2) {
		return null;
	}
	key = infos[1];
	return key;
}


function deleteRecord(key) {
    console.log("deleteRecord() key=" + key );
	var vedioIndex = localStorage['index'];
	var pos = vedioIndex.indexOf(key);
	if(pos != -1) {
		localStorage.removeItem(key);
		var nameArr = vedioIndex.split(",");
		var newIndex = "";
		for(var i = 0; i < nameArr.length; i++) {
			if(nameArr[i] == key) continue;
			if(newIndex.length == 0) {
				newIndex = nameArr[i];
			} else {
				newIndex = newIndex + ","  + nameArr[i];
			}
		}
		localStorage['index'] = newIndex;
		//show();
	}
}

function createRecord(sName, sState, sDate, sLink,sType) {
	var Record = new Object();
	var key = "undefined";
	
	if(sLink.indexOf(prefix) == 0) {
		Record.type = sType;
		Record.name = sName;
		Record.state = sState;
		Record.date = sDate;
		Record.link = sLink;
	
		
		key = sLink.substring(preLen, sLink.length);
	}
	
	if(key.lastIndexOf("/") == (key.length - 1)) 
		key = key.substring(0, key.length - 1);
	
	var infos = key.split("/");
	
	
	if(infos.length != 2) {
		return null;
	}

	key = infos[1];
	Record.key = key;
	var str = JSON.stringify(Record)
	console.log("str:" + str);
	localStorage[key] = str;
	var index = localStorage['index'];
	if(index != null && index.length > 0) {
		if(index.indexOf(key) == -1)
			index = index + "," + key;
	}
	else
		index = key;
	localStorage['index'] = index;
	return Record;
}

function storeViedoStaticInfo(score, logo, lang, note, name) {
	var Info = new Object();
	Info.logo = logo;
	Info.lang = lang;
	Info.note = note;
	Info.name = name;
	Info.score = score;
	var str = JSON.stringify(Info)
	console.log("storeViedoStaticInfo:" + str);
	localStorage[name] = str;
}

function deleteVideoStaticInfo(name) {
	console.log("deleteVideoStaticInfo:" + name);
    localStorage.removeItem(name);
}